﻿using System;
using System.Collections.Generic;
using System.IO;
using SmartWallpaperChanger.Properties;

namespace SmartWallpaperChanger
{
    public class WallpaperList
    {
        private readonly Random random = new Random();
        private readonly List<string> blacklist;
        private readonly List<string> wallpapers;

        /// <summary>
        /// Gets the count of wallpapers.
        /// </summary>
        public int Count
        {
            get { return wallpapers.Count; }
        }

        public WallpaperList()
        {
            random = new Random();
            blacklist = new List<string>();
            wallpapers = new List<string>();
        }

        /// <summary>
        /// Adds a new wallpaper to this collection.
        /// </summary>
        /// <param name="path">path to image file</param>
        public void AddWallpaper(string path)
        {
            if (IsValidImage(path))
            {
                int times = 1;
                TimeSpan age = DateTime.Now.Subtract(File.GetLastAccessTime(path));
                if (age < Settings.Default.TimeUntilOld)
                {
                    times = Settings.Default.Emphasis;
                }

                for (int i = 0; i < times; i++)
                {
                    wallpapers.Add(path);
                }
            }
        }

        /// <summary>
        /// Removes a wallpaper from this collection.
        /// </summary>
        /// <param name="path">path to image file.</param>
        public void RemoveWallpaper(string path)
        {
            while (wallpapers.Contains(path)) wallpapers.Remove(path);
        }

        /// <summary>
        /// Clears this collection of Wallpapers.
        /// </summary>
        public void ClearWallpapers()
        {
            wallpapers.Clear();
        }

        /// <summary>
        /// Checks if this collection contains a wallpaper.
        /// </summary>
        /// <param name="path">path to image file</param>
        /// <returns>true if file is contained</returns>
        public bool ContainsWallpaper(string path)
        {
            return wallpapers.Contains(path);
        }

        /// <summary>
        /// Gets a wallpaper from wallpapers list which is not in blacklist 
        /// and updates the blacklist.
        /// </summary>
        public string GetRandomWallpaper()
        {
            try
            {
                string wallpaper = wallpapers[random.Next(0, wallpapers.Count - 1)];

                while (blacklist.Contains(wallpaper))
                {
                    wallpaper = wallpapers[random.Next(0, wallpapers.Count - 1)];
                }

                blacklist.Insert(0, wallpaper);
                if (blacklist.Count > Settings.Default.Timeout)
                {
                    blacklist.RemoveAt(blacklist.Count - 1);
                }

                return wallpaper;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Verifies if given file is a valid image.
        /// </summary>
        private bool IsValidImage(string path)
        {
            string extension = Path.GetExtension(path);
            if (extension.StartsWith(".")) extension = extension.Substring(1);

            return Settings.Default.Extensions.Contains(extension);
        }
    }
}
